package org.jeecg.modules.yw.service;

import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.jeecg.modules.system.service.ISysAnnouncementSendService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.yw.entity.UserView;
import org.jeecg.modules.yw.mapper.EnterPriseMapper;
import org.jeecg.modules.yw.mapper.SysannoucementMapper;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.vo.SysAnnoucementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Service
public class SysAnnoucementService {
	@Autowired
	private ISysAnnouncementService sysAnnouncementService;
	
	@Autowired
	private SysannoucementMapper sysannoucementMapper;
	
	@Autowired
	private ISysAnnouncementSendService isendService;
	
	
	@Autowired
	private EnterPriseMapper entMapper;
	
	/**
	 * 系统类型, 用户Id
	 * @param sysType 1. 门窗企业注册消息
					2. 安装班组注册消息
					3. 打胶售后注册消息
					4. 门窗企业新建订单
					5. 安装班组接单
					6. 安装班组拒绝
					7. 安装组员拒绝接单
					8. 安装班组反馈问题订单
					9. 安装班组完成
					10. 打胶售后接单
					11. 打胶售后拒绝
					12. 打胶售后质检消息
					13. 打胶售后完成
					14. 结算确认
					15. 员工材料申请消息
					16. 员工请假消息
					17. 员工报损消息
					18. 打胶售后待接单消息
	 * @param userId
	 * @param busiType 1:查损核数，2:洞口复测, 3. 内外铝划伤 4. 木材划伤 5. 玻璃有破损 6 发泡胶有污染 7. 开窗不灵活 8. 执手与扣盖不齐全 9. 发泡胶填充不饱满 10 安装垃圾清理不干净 11.结算确认 12 结算驳回 
	 */
	public void AddSysAnnoucement(String title, String content, String sysType, String createUserId, String userId,
								  String busiId, String busiType, String orderNo, String cityCode) {
		List<UserView> list = entMapper.selectUsers();		
		String userIds = "";
		for (UserView view: list) {
			userIds += view.getId() + ",";
		}
		if (userIds.endsWith(",")) {
			userIds = userIds.substring(0, userIds.length() - 1);
		}		
		
		
		String cityUserIds = "";
		
		List<UserView> cityList = entMapper.selectUserByCityCode(cityCode);
		
		
		for (UserView view: cityList) {
			cityUserIds += view.getId() + ",";
		}
		if (cityUserIds.endsWith(",")) {
			cityUserIds = cityUserIds.substring(0, cityUserIds.length() - 1);
		}
		
		SysAnnouncement sys = new SysAnnouncement();
		sys.setTitile(title);
		sys.setDelFlag("0");
		sys.setMsgContent(content);
		sys.setMsgCategory(sysType);
		if (StringUtils.isBlank(userId)) {
			//userId= 
			sys.setUserIds(userIds);
		} else {
			sys.setUserIds(userId+","+userIds);
		}		
		
		if (StringUtils.isBlank(sys.getUserIds())) {
			if (StringUtils.isNotBlank(cityUserIds)) {
				sys.setUserIds(cityUserIds);
			}
		} else {
			if (StringUtils.isNotBlank(cityUserIds)) {
				sys.setUserIds(cityUserIds + "," + sys.getUserIds()); 
			}
		}		
		
		if (StringUtils.isNotBlank(busiId)) {
			sys.setBusId(busiId);
		}		
	
		
		sys.setInfoNo(orderNo);
		sys.setBusType(busiType);		
		sys.setCreateTime(DateUtils.getCurrentDate());
		sys.setCreateBy(createUserId);		
		sysAnnouncementService.save(sys);
		SysAnnouncementSend sysSend = new SysAnnouncementSend();		
		String uIds = sys.getUserIds();		
		String ids[] = uIds.split(",");
		Set<String> set = new HashSet<>();
		for (String id: ids) {
			if (!set.add(id)) {
				continue;
			}
			sysSend.setAnntId(sys.getId());
			sysSend.setUserId(id);
			sysSend.setReadFlag("0");
			sysSend.setCreateBy(sys.getCreateBy());
			sysSend.setCreateTime(DateUtils.getCurrentDate());
			sysSend.setStarFlag("0");
			isendService.save(sysSend);
		}
	}
	

    public IPage<SysAnnoucementVo> pageEnter(Page<SysAnnoucementVo> page, QueryWrapper<SysAnnoucementVo> queryWrapper) {
        return sysannoucementMapper.msgEnter(page, queryWrapper);
    }
	
	public List<Map<String, String>> getMsgList() {
		// 消息列表
		List<Map<String, String>> allList = new ArrayList<Map<String, String>>();
		
		List<Map<String, String>> registerList = sysannoucementMapper.getEnterpriseCount();
		
		String mcCnt = "0";
		String azbzCnt = "0";
		String djshCnt = "0";
		
		for (Map<String, String> obj : registerList) {
			if (obj.get("roleId") != null && obj.get("roleId").equals("1")) {
				mcCnt = obj.get("cnt").toString();
			} else if (obj.get("roleId") != null && obj.get("roleId").equals("2")) {
				azbzCnt = obj.get("cnt").toString();
			} else if (obj.get("roleId") != null && obj.get("roleId").equals("3")) {
				djshCnt = obj.get("cnt").toString();
			}			
		}		
		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("msgName", "门窗企业注册消息");
		map.put("msgType", "1");
		map.put("cnt", mcCnt);
		map.put("url", "/enterprise/list?roleId=1");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "安装班组注册消息");
		map.put("msgType", "2");
		map.put("cnt", azbzCnt);
		map.put("url", "/enterprise/list?roleId=2");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "打胶售后注册消息");
		map.put("msgType", "3");
		map.put("cnt", djshCnt);
		map.put("url", "/enterprise/list?roleId=3");
		allList.add(map);
		
		Integer newOrder = sysannoucementMapper.getNewOrderCnt();		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "门窗企业新建订单");
		map.put("msgType", "4");
		map.put("cnt", newOrder == null?"0":newOrder.toString());
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		List<Map<String, String>> noticeList = sysannoucementMapper.getMessageCnt();
		
		String azbzjdCnt = "0";
		String azbzjjCnt = "0";
		String azzyCnt = "0";
		String azbzwtCnt = "0";
		String azbzwcCnt = "0";
		String djshjdCnt = "0";
		
		String djshjjCnt = "0";
		
		String djshzjCnt = "0";
		
		String djshwcCnt = "0";
		
		String jsqrCnt = "0";
		
		String ygclsqCnt = "0";
		 
		String ygqjsqCnt = "0";
		
		String ygbsCnt = "0";
		
		String djshdjCnt = "0";
		
		for (Map<String, String> obj : noticeList) {
			String msgType = obj.get("msgType")==null?"":obj.get("msgType").toString();
			if (msgType != null && msgType.equals("5")) {
				azbzjdCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			} else if (msgType != null && msgType.equals("6")) {
				azzyCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			} else if (msgType != null && msgType.equals("7")) {
				azbzjjCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			} else if (msgType != null && msgType.equals("8")) {
				azbzwtCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("9")) {
				azbzwcCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("10")) {
				djshjdCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("11")) {
				djshjjCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("12")) {
				djshzjCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			} else if (msgType != null && msgType.equals("13")) {
				djshwcCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("14")) {
				jsqrCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("15")) {
				ygclsqCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("16")) {
				ygqjsqCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}  else if (msgType != null && msgType.equals("17")) {
				ygbsCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}   else if (msgType != null && msgType.equals("18")) {
				djshdjCnt =  obj.get("cnt")==null?"":obj.get("cnt").toString();
			}   				
		}
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "安装班组接单");
		map.put("msgType", "5");
		map.put("cnt",  azbzjdCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "安装班组拒绝接单");
		map.put("msgType", "6");
		map.put("cnt",  azbzjjCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "安装组员拒绝接单");
		map.put("msgType", "7");
		map.put("cnt",  azzyCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "安装班组反馈问题订单");
		map.put("msgType", "8");
		map.put("cnt",  azbzwtCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);

		map = new LinkedHashMap<String, String>();
		map.put("msgName", "安装班组完成");
		map.put("msgType", "9");
		map.put("cnt",  azbzwcCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		

		map = new LinkedHashMap<String, String>();
		map.put("msgName", "打胶售后接单");
		map.put("msgType", "10");
		map.put("cnt",  djshjdCnt);
		map.put("url", "/orderManage/list0");
		allList.add(map);
		
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "打胶售后拒绝");
		map.put("msgType", "11");
		map.put("cnt",  djshjjCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "打胶售后质检消息");
		map.put("msgType", "12");
		map.put("cnt",  djshzjCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "打胶售后完成");
		map.put("msgType", "13");
		map.put("cnt",  djshwcCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "结算确认");
		map.put("msgType", "14");
		map.put("cnt",  jsqrCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "员工材料申请消息");
		map.put("msgType", "15");
		map.put("cnt",  ygclsqCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "员工请假申请");
		map.put("msgType", "16");
		map.put("cnt",  ygqjsqCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "员工报损消息");
		map.put("msgType", "17");
		map.put("cnt",  ygbsCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		map = new LinkedHashMap<String, String>();
		map.put("msgName", "打胶售后待接订单消息");
		map.put("msgType", "18");
		map.put("cnt",  djshdjCnt);
		map.put("url", "/orderManage/list");
		allList.add(map);
		
		return allList;
	}
	
	/**
	 * 得到信息数量
	 * @param msgId
	 * @return
	 */
	public int getMsgCountByMsgId(Integer msgId) {		
		return 0;
	}
	public int getDjShJjMessageList(String busiId){
		Integer djShJjMessageList = sysannoucementMapper.getDjShJjMessageList(busiId);
		if (djShJjMessageList == null){
			return 0;
		}
		return djShJjMessageList;
	}
	
	public int updateMsg(String id) {
		return sysannoucementMapper.updateMsg(id);
	}
	
	
	public int updateRead(String busiId) {
		return sysannoucementMapper.updateRead(busiId);
	}
}
